#include <iostream>
#include <string>
#include <memory>
#include "Socket.hpp"
#include "TcpServer.hpp"
#include "Accepter.hpp"
using namespace Net_Work;

static int defaultbacklog = 32;

void Usage(std::string proc)
{
    std::cout << proc << " server port" << std::endl;
}

void Test(Connection *conn)
{
    lg.LogMessage(Info, "get a new conn,fd %d", conn->GetConnFd());
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }
    int serverport = std::stoi(argv[1]);

    // 1.创建listen套接字
    std::unique_ptr<Socket> listensock = std::make_unique<TcpSocket>();
    listensock->BuildListenSocketMethod(serverport, defaultbacklog);

    // 2.创建tcp
    std::unique_ptr<TcpServer> svr = std::make_unique<TcpServer>();
    std::unique_ptr<Accepter> accepter = std::make_unique<Accepter>();
    
    auto listen_recver = std::bind(&Accepter::AcceptConnection, accepter.get(), std::placeholders::_1);

    // 3.创建监听connect
    Connection *conn = ConnectionFactory::BulidListenConnection(listensock->GetSockfd(), listen_recver, EPOLLIN | EPOLLET, svr.get());
    svr->AddConnection(conn);

    svr->Dispatcher();

    return 0;
}